package com.cskaoyan.service.wx_cart;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.bean.bo.wx_cart.CollectAddOrDelBO;
import com.cskaoyan.bean.bo.wx_cart.CollectListBO;
import com.cskaoyan.bean.po.ohter.MarketCollect;
import com.cskaoyan.bean.po.ohter.MarketCollectExample;
import com.cskaoyan.bean.po.wx_cart.CollectGoodsInfo;
import com.cskaoyan.bean.po.wx_cart.CollectInfo;
import com.cskaoyan.bean.vo.wx_cart.CollectListVO;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.wx_cart.CollectGoodsMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.Constant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: market
 * @description: 微信收藏实现类注册组件
 * @author: zhong
 * @create: 2022-07-21 15:02
 **/
@Service
public class WxCollectServiceImpl implements WxCollectService {
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    CollectGoodsMapper collectGoodsMapper;

    @Override
    public CollectListVO listCollectOp(CollectListBO pageReg, Integer userId) {
        PageHelper.startPage(pageReg.getPage(), pageReg.getLimit());

        List<CollectInfo> collectInfos = marketCollectMapper.queryCollectGoods(userId, pageReg.getType());

        PageInfo<CollectInfo> pageInfo = new PageInfo<>(collectInfos);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return new CollectListVO(total, pages, pageReg.getLimit(), pageReg.getPage(), collectInfos);
    }

    @Override
    public int updateCollectOp(CollectAddOrDelBO collectBO, Integer userId) {
        //查收藏
        MarketCollectExample target = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = target.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andValueIdEqualTo(collectBO.getValueId());
        criteria.andTypeEqualTo(collectBO.getType().byteValue());
        criteria.andDeletedEqualTo(false);
        long countExist = marketCollectMapper.countByExample(target);
        if (countExist!= 0) {
            //已有此商品的收藏记录，全部更新deleted,取消收藏
            MarketCollect collect = new MarketCollect();
            collect.setDeleted(true);
            collect.setUpdateTime(new Date());
            marketCollectMapper.updateByExampleSelective(collect, target);
            //del ok
            return Constant.OK_CODE;
        }
        //收藏中没有记录，不管collect中有没有已删除的该商品，从goods取信息后添加最新信息的收藏
        QueryWrapper<CollectGoodsInfo> goodsWrapper = new QueryWrapper<>();
        goodsWrapper.eq("`id`", collectBO.getValueId());
        goodsWrapper.eq("`deleted`", 0);
        CollectGoodsInfo goodsInfo = collectGoodsMapper.selectOne(goodsWrapper);
        if (goodsInfo == null) {
            //商品已删除,或下架
            return Constant.NOT_FOUND_CODE;
        }
        MarketCollect addTarget = new MarketCollect();
        addTarget.setAddTime(new Date());
        addTarget.setUpdateTime(addTarget.getAddTime());
        addTarget.setDeleted(false);
        addTarget.setType(collectBO.getType().byteValue());
        addTarget.setUserId(userId);
        addTarget.setValueId(collectBO.getValueId());
        int addRow = marketCollectMapper.insertSelective(addTarget);
        //add ok
        return Constant.CREATED_CODE;
    }
}
